<template>
	<om-formsub-outer :index="index" :checkedit="checkedit">
		<om-autoform ref="autoform" :edit="edit" :checkedit="checkedit" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {
		buildSubFormData,
		subOnLoad,
		changeEditor,
		configSelectData
	} from '../../../static/js/formUtil.js';
	import {
		ignoreHtml,
		ptFloat,
		minusF,
		getDateStr
	} from '@/static/js/common.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import  moment from "@/static/js/moment.js"
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildSubFormData({
				year:'',
				userId:'',
				posId:'',
				exists:'',
				editId:'',
				indexCacheKey:'obj-zb-tindexAdjust-title_new-index-cache'
			})
		},
		methods:{
			onInputMonth1Code(value){
				let _that = this
				_that.$refs.autoform.formdata.month1Name =  value + _that.$refs.autoform.formdata.indexUnitName
			},
			onInputMonth2Code(value){
				let _that = this
				_that.$refs.autoform.formdata.month2Name =  value + _that.$refs.autoform.formdata.indexUnitName
			},
			onInputMonth3Code(value){
				let _that = this
				_that.$refs.autoform.formdata.month3Name =  value + _that.$refs.autoform.formdata.indexUnitName
			},
			onInputMonth4Code(value){
				let _that = this
				_that.$refs.autoform.formdata.month4Name =  value + _that.$refs.autoform.formdata.indexUnitName
			},
			onInputMonth5Code(value){
				let _that = this
				_that.$refs.autoform.formdata.month5Name =  value + _that.$refs.autoform.formdata.indexUnitName
			},
			onInputMonth6Code(value){
				let _that = this
				_that.$refs.autoform.formdata.month6Name =  value + _that.$refs.autoform.formdata.indexUnitName
			},
			onInputMonth7Code(value){
				let _that = this
				_that.$refs.autoform.formdata.month7Name =  value + _that.$refs.autoform.formdata.indexUnitName
			},
			onInputMonth8Code(value){
				let _that = this
				_that.$refs.autoform.formdata.month8Name =  value + _that.$refs.autoform.formdata.indexUnitName
			},
			onInputMonth9Code(value){
				let _that = this
				_that.$refs.autoform.formdata.month9Name =  value + _that.$refs.autoform.formdata.indexUnitName
			},
			onInputMonth10Code(value){
				let _that = this
				_that.$refs.autoform.formdata.month10Name =  value + _that.$refs.autoform.formdata.indexUnitName
			},
			onInputMonth11Code(value){
				let _that = this
				_that.$refs.autoform.formdata.month11Name =  value + _that.$refs.autoform.formdata.indexUnitName
			},
			onInputMonth12Code(value){
				let _that = this
				_that.$refs.autoform.formdata.month12Name =  value + _that.$refs.autoform.formdata.indexUnitName
			},
			
			onInputWeight(val){
				let _that = this
				if(Number(val) > 100){
					_that.$refs.autoform.formdata.weight = 100
				}
			},
			clearChecks(){
				let _that = this
				_that.$refs.autoform.clear("title_new","weight")
				_that.$refs.autoform.clear("title_new","checksId")
				_that.$refs.autoform.changeEditor("title_new","weight","type","labelbox")
			},
			onSelectChecks(item){
				let _that  = this
				_that.$refs.autoform.changeEditor("title_new","weight","type","numberbox")
				_that.$refs.autoform.changeEditor('title_new',"weight","digit",true)
				_that.$refs.autoform.changeEditor("title_new","weight","prompt",'请输入')
			},
			changeType(d){
				let _that = this
				_that.$refs.autoform.changeEditor("title_new","targetValueCode","type",'numberbox')
				_that.$refs.autoform.changeEditor("title_new","targetValueCode","prefix",d.logicRelationshipName)
				_that.$refs.autoform.changeEditor("title_new","targetValueCode","suffix",d.indexUnitName)
				_that.$refs.autoform.changeEditor("title_new","targetValueCode","suffixCol",d.indexUnitName.length)
				_that.$refs.autoform.changeEditor("title_new","targetValueCode","digit",true)
				let month = moment().month()
				for(let index = 0; index<12;index++){
					if(index+1 > month){
						_that.$refs.autoform.changeEditor('title_new',"month"+(index+1)+"Code","digit",true)
						_that.$refs.autoform.changeEditor("title_new","month"+(index+1)+"Code","type","numberbox")
						_that.$refs.autoform.changeEditor("title_new","month"+(index+1)+"Code","suffix",d.indexUnitName)
						_that.$refs.autoform.changeEditor("title_new","month"+(index+1)+"Code","suffixCol",d.indexUnitName.length)
						_that.$refs.autoform.changeEditor("title_new","month"+(index+1)+"Code","prompt",'请输入')
					}
				}
			},
			
			verifyQuarter(id){
				let _that = this
				_that.om.request(
					"torgAnnualTargetPlan/verifyQuarter",
					{
						indexId:id
					}
				).then(res=>{
					if(res.value){
						for(let i = 1;i<5;i++){
							_that.$refs.autoform.changeEditor('title_new','quarter'+i+'Code','type','numberbox')
							_that.$refs.autoform.changeEditor('title_new','quarter'+i+'Code',"digit",true)
							_that.$refs.autoform.changeEditor('title_new','quarter'+i+'Code',"prompt",'请输入')
							_that.$refs.autoform.changeEditor('title_new','quarter'+i+'Code',"suffix",'%')
						}
					}
				})
			},
			
			getCategoryList(id){
				let _that = this
				_that.om.request(
					"indexDict/getCategoryList",
					{
						indexId: id
					}
				).then(res=>{
					_that.$refs.autoform.changeEditor("title_new","busRange","type","selectbox")
					_that.$refs.autoform.changeEditor("title_new","busRange","prompt","请选择")
					if(res.length){
						_that.$refs.autoform.changeEditor("title_new","busRange","data",configSelectData(res,'name','value'))
					}else{
						_that.$refs.autoform.changeEditor("title_new","busRange","data",configSelectData([],'name','value'))
					}
					_that.$refs.autoform.initSelectIndex("title_new","busRange")
				})
			},
			initload(){
				let _that = this
				let arr = [
					_that.om.request(
						"torgAnnualTargetPlan/getCheckList",
						{},
						'POST'
					)
				]
				Promise.all(arr).then(res=>{
					_that.$refs.autoform.changeEditor("title_new","checksId","data",configSelectData(res[0],"id","text"))
					_that.$refs.autoform.initSelectIndex("title_new","checksId")
					_that.$refs.autoform.changeEditor("title_new","checksId","onSelect","onSelectChecks")
				})
			},
		},
		onLoad(e) {
			let _that = this
			_that.year = e.year
			_that.userId = e.userId
			_that.posId = e.posId
			_that.editId = e.editId
			subOnLoad(_that, e, function() {
				_that.exists = _that.datas.filter((e,i)=>{
					return (i != _that.index)
				}).map(e=>e.indexId).join(",")
				if (e.configDataCacheKey) {
					uni.getStorage({
						key: e.configDataCacheKey,
						success: function(res) {
							//if (res.data && res.data instanceof Array) {
							if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
								_that.autoformdata = res.data
							}
							Object.assign(_that.initValue, _that.formdata)
							_that.autoformdata[0].order = _that.order
							changeEditor('title_new',"indexId",{
								link:'../../public/pageSearch/pageSearch',
								"linkParam":{
									other: JSON.stringify({
										year: _that.year,
										userId: _that.userId,
										posId: _that.posId,
										notInStr: _that.exists,
										editId: _that.editId
									}),
									initload: true,
									method:'POST',
									namefield:'dicname',
									searchfield:'q',
									notefield:'indexUnitName',
									url:'tindexAdjust/getNewIndexList',
									cachename: _that.indexCacheKey,
								}
							},null,_that.autoformdata)
							_that.initload()
						}
					})
				}
			})
		},
		onShow() {
			let _that = this
			if (_that.$refs.autoform) {
				uni.getStorage({
					key:_that.indexCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.indexId = d.id
							formdata.indexName = d.dicname
							formdata.orderNum = d.orderNum
							formdata.logicRelationshipName = d.logicRelationshipName
							formdata.indexUnitName = d.indexUnitName
							_that.changeType(d)
							_that.getCategoryList(d.id)
							_that.verifyQuarter(d.id)
							uni.removeStorage({
								key:_that.indexCacheKey
							})
						}
					}
				})
			}
		},
		onReady(){
			let _that = this
			_that.$nextTick(function(){
				if(_that.$refs.autoform.formdata.indexId){
					let d = _that.$refs.autoform.formdata 
					_that.changeType(d)
					_that.getCategoryList(d.indexId)
					_that.verifyQuarter(d.indexId)
				}
				if(_that.$refs.autoform.formdata.checksId){
					_that.onSelectChecks()
				}
			})
		}
	}
</script>

<style>
</style>
